﻿namespace Forms
{
    using System.Linq;

    using Forms.My;

    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using Microsoft.VisualBasic.FileIO;

    using System;
    using System.Collections;
    using System.Data;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Text;

    public class Session
    {
        public object DataBaseNeededVersion = 2;
        public DatenschemaBMW dsBMW = new DatenschemaBMW();
        public string LeseProtokoll;
        public string ProtokollSymbol;
        public string Zusammenfassung;

        public bool BackupFiles(string Path, string BackupPath)
        {
            string destFileName = "";
            if (Directory.Exists(Path) & Directory.Exists(BackupPath))
            {
                MyProject.Forms.Main.StatusProgress.Maximum = Directory.GetFiles(Path).Length;
                string str2 = Strings.Format(DateAndTime.Now, "dd-MM-yyyy HH-mm");
                foreach (string str3 in Directory.GetFiles(Path))
                {
                    if (this.GetHeader(str3).ftType != FileTypes.ftUnknown)
                    {
                        try
                        {

                            destFileName = BackupPath + @"\" + System.IO.Path.GetFileNameWithoutExtension(str3) + " " + str2 + "." + System.IO.Path.GetExtension(str3);
                            File.Move(str3, destFileName);
                        }
                        catch (IOException exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            IOException exception = exception1;
                            Interaction.MsgBox("Die Datei '" + destFileName + "' existiert bereits! Es wurde keine Sicherung von '" + str3 + "' erstellt.", MsgBoxStyle.Exclamation, "Zieldatei existiert bereits");
                            ProjectData.ClearProjectError();
                        }
                        catch (UnauthorizedAccessException exception3)
                        {
                            ProjectData.SetProjectError(exception3);
                            UnauthorizedAccessException exception2 = exception3;
                            Interaction.MsgBox("Es besteht ein Rechteproblem beim Sichern der Import-Dateien. Prüfen Sie, ob Sie alle nötigen Zugriffsrechte für die betreffenden Pfade besitzen ('" + BackupPath + "') und ob die betreffende Datei ('" + str3 + "') von keinem anderen Programm geöffnet ist.", MsgBoxStyle.Exclamation, "Zugriffsproblem");
                            ProjectData.ClearProjectError();
                        }
                    }
                    MyProject.Forms.Main.StatusProgress.PerformStep();
                    MyProject.Forms.Main.Refresh();
                }
                return true;
            }
            return false;
        }
        /// <summary>
        /// Setzt die Bemerkungen in den jeweiligen Tabellen
        /// </summary>
        public void BemerkungenLesen()
        {
            var changes = dsBMW.TAB_Bemerkungen.GetChanges(DataRowState.Modified);
            if (changes == null)
            {
                return;
            }

            foreach (DataRow current in changes.Rows)
            {
                if (current.RowState == DataRowState.Deleted)
                {
                    continue;
                }

                var rows =
                    this.dsBMW.TAB_Auswertung_Bestand.Select(
                        string.Format("TRIM(FGST_Nr) like '%{0}'", current["FGST_Nr"].ToString().Trim()));
                foreach (var dataRow in rows)
                {
                    dataRow["Bemerkung"] = current["Bemerkung"].ToString();
                    break;
                }

                rows =
                    this.dsBMW.TAB_Auswertung_Bestand.Select(
                        string.Format("TRIM(FGST_Nr) like '%{0}'", current["FGST_Nr"].ToString().Trim()));
                foreach (var dataRow in rows)
                {
                    dataRow["Bemerkung"] = current["Bemerkung"].ToString();
                    break;
                }
                rows =
                    this.dsBMW.TAB_BMW_Bank_Details.Select(
                        string.Format("TRIM(FGST_Nr) like '%{0}'", current["FGST_Nr"].ToString().Trim()));
                foreach (var dataRow in rows)
                {
                    dataRow["Bemerkung"] = current["Bemerkung"].ToString();
                    break;
                }


                rows =
                    this.dsBMW.TAB_F1_Detail.Select(
                        string.Format("TRIM(FGST_Nr) like '%{0}'", current["FGST_Nr"].ToString().Trim()));
                foreach (var dataRow in rows)
                {
                    dataRow["Bemerkung"] = current["Bemerkung"].ToString();
                    break;
                }



                rows =
                    this.dsBMW.TAB_Bestand_Details.Select(
                        string.Format("TRIM(FGST_Nr) like '%{0}'", current["FGST_Nr"].ToString().Trim()));
                foreach (var dataRow in rows)
                {
                    dataRow["Bemerkung"] = current["Bemerkung"].ToString();
                    break;
                }

            }
        }
        /// <summary>
        ///  Speichert die Bemerkungen.
        /// </summary>
        public void BemerkungenSpeichernAuswertung()
        {
            // TASK: Enumerierung der Einträge kann sehr lange dauern, daher sollten hier die geänderten Zeilen selektiert werden. 
            //            Jedoch werden die nicht als Changed markiert, daher ist es nicht möglich diese hier zu selektieren. Es ist also später zu optimieren.
            IEnumerator enumerator = this.dsBMW.TAB_Auswertung.GetEnumerator();
            try
            {
                
                while (enumerator.MoveNext())
                {
             
                    DataRow current = (DataRow) enumerator.Current;

                    // wenn Fahrzeug nummer weniger als 7 Stellen besitzt, dann überspringe!
                    if (current["FGST_Nr"].ToString().Length < 7)
                    {
                        continue;
                    }

                    bool flag = false;
                    // selektiere mir die Zeile mit der entsprechenden Fahrgestellnummer.
                    var selectedRow = this.dsBMW.TAB_Bemerkungen.Select(string.Format("FGST_Nr='{0}'", current["FGST_Nr"]));

                    // nur nicht gelöschte zeilen selektieren.
                    foreach (var dataRow in selectedRow.Where(i=>i.RowState != DataRowState.Deleted))
                    {
                        // Die Bemerkung wurde von dem Benutzer oder im system entfernt
                        if (current["Bemerkung"] == DBNull.Value || string.IsNullOrEmpty(dataRow["Bemerkung"].ToString()))
                        {
                            // lösche die datarow wenn in der Zielspalte die Bemerkung nicht gesetzt ist.
                            dataRow.Delete();
                        }
                        else
                        {
                            dataRow["Bemerkung"] = RuntimeHelpers.GetObjectValue(current["Bemerkung"]);
                        }
                        flag = true;
                        // nach der ersten Zeile rausspringen!
                        break;
                    }

                    // wenn die Bemerkung nicht DBNull ist 
                    // und ein update der Spalte erfolgt 
                    // UND der inhalt nicht "" ist
                    if ((!flag && (current["Bemerkung"] != DBNull.Value)) && Operators.ConditionalCompareObjectNotEqual(current["Bemerkung"], "", false))
                    {
                        // Dann füge in Tabelle Bemerkungen eine neue Zeile ein mit der Fahrgestellnummer und der Bemerkung.
                        DataRow row = this.dsBMW.TAB_Bemerkungen.NewRow();
                        row["FGST_Nr"] = Strings.Right(Conversions.ToString(current["FGST_Nr"]), 7);
                        row["Bemerkung"] = RuntimeHelpers.GetObjectValue(current["Bemerkung"]);
                        this.dsBMW.TAB_Bemerkungen.Rows.Add(row);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
                this.dsBMW.TAB_Auswertung.AcceptChanges();
                this.dsBMW.TAB_Bemerkungen.AcceptChanges();
            }
            this.BemerkungenLesen();
        }

        public void BemerkungenSpeichernAuswertungBestand()
        {
            IEnumerator enumerator = this.dsBMW.TAB_Auswertung_Bestand.GetEnumerator();
            try
            {
               
                while (enumerator.MoveNext())
                {
                    
                    DataRow current = (DataRow) enumerator.Current;
                    if (current["FGST_Nr"].ToString().Length < 7)
                    {
                        continue;
                    }
                    bool flag = false;

                    var rows =  dsBMW.TAB_Bemerkungen.Select(string.Format("FGST_Nr='{0}'", current["FGST_Nr"]));
                    foreach (var dataRow in rows.Where(i=>i.RowState != DataRowState.Deleted) )
                    {
                        if ( current["Bemerkung"] == DBNull.Value || string.IsNullOrEmpty(current["Bemerkung"].ToString()))
                        {
                            dataRow.Delete();
                        }
                        else
                        {
                            dataRow["Bemerkung"] = current["Bemerkung"].ToString();
                               
                        }
                        flag = true;
                        break;
                    }

                    if ((!flag && (current["Bemerkung"] != DBNull.Value)) && Operators.ConditionalCompareObjectNotEqual(current["Bemerkung"], "", false))
                    {
                        DataRow row = this.dsBMW.TAB_Bemerkungen.NewRow();
                        row["FGST_Nr"] = Strings.Right(Conversions.ToString(current["FGST_Nr"]), 7);
                        row["Bemerkung"] = RuntimeHelpers.GetObjectValue(current["Bemerkung"]);
                        this.dsBMW.TAB_Bemerkungen.Rows.Add(row);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            this.BemerkungenLesen();
        }

        public void ClearAbloeseHistorie()
        {
            int num2 = DateAndTime.Year(DateAndTime.Now) - 1;
            DateTime time = Convert.ToDateTime("31.12." + ((num2 - 1)).ToString());
            DataView view = new DataView(this.Table_Historie, "Abloese_Datum<=" + time.ToString(@"#MM\/dd\/yyyy#"), "", DataViewRowState.CurrentRows);

            IEnumerator enumerator = view.GetEnumerator();
           
            try
            {
                
                while (enumerator.MoveNext())
                {
                    DataRowView current = (DataRowView) enumerator.Current;
                    current.Row.Delete();
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            this.Table_Historie.AcceptChanges();
            view.Dispose();
        }

        public void ClearData()
        {
            this.dsBMW.TAB_Auswertung.Clear();
            this.dsBMW.TAB_Auswertung_Bestand.Clear();
            this.dsBMW.TAB_AuswertungTitle.Clear();
            this.dsBMW.TAB_BMW_Bank_Details.Clear();
            this.dsBMW.TAB_BMW_Bank_Header.Clear();
            this.dsBMW.TAB_F1_Detail.Clear();
            this.dsBMW.TAB_F1_Header.Clear();
            this.dsBMW.TAB_Bestand_Details.Clear();
            this.dsBMW.TAB_Bestand_Header.Clear();
            this.dsBMW.TAB_BMW_Bank_Details_Alle_TMP.Clear();
        }

        public void ClearDataReload()
        {
            this.dsBMW.TAB_Auswertung.Clear();
            this.dsBMW.TAB_Auswertung_Bestand.Clear();
        }

        /// <summary>
        /// List die Dauer Fahrzeuge aus, und speichert die Fahrzeug ID's zu den finanzarten.
        /// </summary>
        public void DauerFAsLesen()
        {
            IEnumerator enumerator = this.dsBMW.TAB_DauerFAs.GetEnumerator();
            try
            {
                
                while (enumerator.MoveNext())
                {
                    //IEnumerator enumerator2 = this.dsBMW.TAB_Auswertung.GetEnumerator();
                    DataRow current = (DataRow) enumerator.Current;

                    var auswertung = this.dsBMW.TAB_Auswertung.Select(string.Format("TRIM(FGST_Nr) like '%{0}'", current["FGST_Nr"].ToString().Trim()));
                    if (auswertung.Count()>0)
                    {
                        continue;
                    }

                    var finanzierungen = this.dsBMW.TAB_FinanzArten.Select(string.Format("FA_ID='{0}'", current["FA_ID"]));
                    if (finanzierungen.Count()>0 )
                    {

                        // setze die Fahrzeug ID.
                        finanzierungen[0]["FA_ID"] = current["FA_ID"];
                        // den importdatensatz löschen.
                        current.Delete();
                    }
                    #region old code
                    //try
                    //{
                        
                        //while (enumerator2.MoveNext())
                        //{
                        //    DataRow row2 = (DataRow) enumerator2.Current;
                        //    try
                        //    {
                                
                        //        if (!row2["FGST_Nr"].ToString().Trim().EndsWith(current["FGST_Nr"].ToString().Trim()))
                        //        {
                        //            continue;
                        //        }
                        //        bool flag = false;

                        //        IEnumerator enumerator3 = this.dsBMW.TAB_FinanzArten.GetEnumerator();
                        //        try
                        //        {
                                    
                        //            while (enumerator3.MoveNext())
                        //            {
                        //                DataRow row3 = (DataRow) enumerator3.Current;
                        //                if (Operators.ConditionalCompareObjectEqual(row3["FA_ID"], current["FA_ID"], false))
                        //                {
                        //                    flag = true;
                        //                    goto Label_00E2;
                        //                }
                        //            }
                        //        }
                        //        finally
                        //        {
                        //            if (enumerator3 is IDisposable)
                        //            {
                        //                (enumerator3 as IDisposable).Dispose();
                        //            }
                        //        }
                        //    Label_00E2:
                        //        if (flag)
                        //        {
                        //            row2["FA_ID"] = RuntimeHelpers.GetObjectValue(current["FA_ID"]);
                        //        }
                        //        else
                        //        {
                        //            current.Delete();
                        //        }
                        //        break;
                        //    }
                        //    catch (Exception exception1)
                        //    {
                        //        ProjectData.SetProjectError(exception1);
                        //        ProjectData.ClearProjectError();
                        //        continue;
                        //    }
                        //}
                    //    continue;
                    //}
                    //finally
                    //{
                    //    if (enumerator2 is IDisposable)
                    //    {
                    //        (enumerator2 as IDisposable).Dispose();
                    //    }
                    //}
                    #endregion
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
                dsBMW.TAB_DauerFAs.AcceptChanges();
            }

        }

        public void DauerFAsSpeichern()
        {
            IEnumerator enumerator = this.dsBMW.TAB_Auswertung.GetEnumerator();
            try
            {
                
                while (enumerator.MoveNext())
                {
                    //IEnumerator enumerator2 = this.dsBMW.TAB_DauerFAs.GetEnumerator();
                    DataRow current = (DataRow) enumerator.Current;
                    if (current["FGST_Nr"].ToString().Length < 7)
                    {
                        continue;
                    }
                    bool found = false;
                    //try
                    //{
                        var rows = dsBMW.TAB_DauerFAs.Select(string.Format("FGST_Nr = '{0}'", current["FGST_Nr"]));
                        if (rows.Count()>0)
                        {
                            continue;
                        }
                        foreach (DataRow row in rows)
                        {
                            if (current["FA_ID"] != DBNull.Value)
                            {
                                if (Operators.ConditionalCompareObjectEqual(current["FA_ID"], 0, false))
                                {
                                    row.Delete();
                                }
                                else
                                {
                                    row["FA_ID"] = RuntimeHelpers.GetObjectValue(current["FA_ID"]);
                                }
                            }
                            else
                            {
                                row.Delete();
                            }
                            found = true;
                        }
#region Old Code
                    //    while (enumerator2.MoveNext())
                    //    {
                    //        DataRow row2 = (DataRow) enumerator2.Current;
                    //        try
                    //        {
                    //            if (!Operators.ConditionalCompareObjectEqual(row2["FGST_Nr"], current["FGST_Nr"], false))
                    //            {
                    //                continue;
                    //            }
                    //            if (current["FA_ID"] != DBNull.Value)
                    //            {
                    //                if (Operators.ConditionalCompareObjectEqual(current["FA_ID"], 0, false))
                    //                {
                    //                    row2.Delete();
                    //                }
                    //                else
                    //                {
                    //                    row2["FA_ID"] = RuntimeHelpers.GetObjectValue(current["FA_ID"]);
                    //                }
                    //            }
                    //            else
                    //            {
                    //                row2.Delete();
                    //            }
                    //            flag = true;
                    //            goto Label_010F;
                    //        }
                    //        catch (Exception exception1)
                    //        {
                    //            ProjectData.SetProjectError(exception1);
                    //            ProjectData.ClearProjectError();
                    //            continue;
                    //        }
                    //    }
                    //}
                    //finally
                    //{
                    //    if (enumerator2 is IDisposable)
                    //    {
                    //        (enumerator2 as IDisposable).Dispose();
                    //    }
                    //}
                //Label_010F:
#endregion
                    if ((!found && (current["FA_ID"] != DBNull.Value)) && Operators.ConditionalCompareObjectNotEqual(current["FA_ID"], 0, false))
                    {
                        DataRow row = this.dsBMW.TAB_DauerFAs.NewRow();
                        row["FGST_Nr"] = Strings.Right(Conversions.ToString(current["FGST_Nr"]), 7);
                        row["FA_ID"] = RuntimeHelpers.GetObjectValue(current["FA_ID"]);
                        this.dsBMW.TAB_DauerFAs.Rows.Add(row);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            this.DauerFAsLesen();
        }

        public void DSConvert()
        {
            DataSet set = new DataSet();
            set.ReadXml(MySettings.MySettingsProperty.DatabasePath + @"\amDataBase.xml");
            IEnumerator enumerator = set.Tables.GetEnumerator();
            try
            {
                
                while (enumerator.MoveNext())
                {
                    DataTable current = (DataTable) enumerator.Current;
                    bool flag = true;
                    switch (current.TableName)
                    {
                        case "TAB_Bemnerkungen":
                            flag = false;
                            break;

                        case "TAB_AuswertungTitle":
                            flag = false;
                            break;

                        case "TAB_FinanzArten":
                            flag = false;
                            break;

                        case "TAB_BMW_Debitorenkonten":
                            flag = false;
                            break;

                        case "TAB_Historie":
                            flag = false;
                            break;

                        case "TAB_DauerFAs":
                            flag = false;
                            break;
                    }
                    if (flag)
                    {
                        current.Clear();
                        current.AcceptChanges();
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            set.AcceptChanges();
            set.WriteXml(MySettings.MySettingsProperty.DatabasePath + @"\amDataBase.xml");
            set.Dispose();
        }

        internal int GetBMWHeaderID(long DebKto)
        {
            string str = DebKto.ToString().Trim();
            IEnumerator enumerator = this.Table_BMWBankHeaders.Rows.GetEnumerator();
            try
            {
                
                while (enumerator.MoveNext())
                {
                    DatenschemaBMW.TAB_BMW_Bank_HeaderRow current = (DatenschemaBMW.TAB_BMW_Bank_HeaderRow) enumerator.Current;
                    if (current.Deb_Kto_Nr.Trim() == str)
                    {
                        return (int) current.BMW_Header_ID;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return -1;
        }

        internal int GetBMWKontoDetailsCount(long DebKto)
        {
            int num = -1;
            int length = 0;
            int bMWHeaderID = this.GetBMWHeaderID(DebKto);
            if (bMWHeaderID != -1)
            {
                DataRow[] rowArray = this.Table_BMWBankDetails.Select("BMW_Header_ID=" + bMWHeaderID.ToString(), "", DataViewRowState.CurrentRows);
                if (rowArray != null)
                {
                    length = rowArray.Length;
                }
            }
            return num;
        }

        internal bool GetFilesINet()
        {
            bool flag2 = true;
            DataRow[] rowArray = this.Table_BMW_Debitorenkonten.Select("Abrufen=True", "Deb_Kto_Nr", DataViewRowState.CurrentRows);
            if (rowArray.Length > 0)
            {
                this.LeseProtokoll = this.LeseProtokoll + "Lese BMW-Bankkkonten per Internetabruf\n\n";
                foreach (DataRow row in rowArray)
                {
                    DatenschemaBMW.TAB_BMW_Bank_HeaderRow row2 = (DatenschemaBMW.TAB_BMW_Bank_HeaderRow) this.Table_BMWBankHeaders.NewRow();
                    DatenschemaBMW.TAB_BMW_Bank_HeaderRow row3 = row2;
                    row3.FileName = "Internet";
                    row3.Name = Conversions.ToString(Operators.AddObject("Internet -", row["Bezeichnung"]));
                    row3.Deb_Kto_Nr = Conversions.ToString(row["Deb_Kto_Nr"]);
                    row3.Ort = Conversions.ToString(row["Bezeichnung"]);
                    row3.LastChanged = Strings.Format(DateAndTime.Now, "dd.MM.yyyy HH:mm");
                    row3.Typ = "BMW";
                    row3 = null;
                    this.Table_BMWBankHeaders.Rows.Add(row2);
                }
                this.Table_BMWBankHeaders.AcceptChanges();
                frmKontenrundruf kontenrundruf = new frmKontenrundruf();
                kontenrundruf.ShowDialog();
                foreach (KontenRundrufData data in kontenrundruf.KontenRundrufListe)
                {
                    if (!data.Done | data.Fehler)
                    {
                        flag2 = false;
                        this.LeseProtokoll = this.LeseProtokoll + "\n Konto [Filename]" + data.Konto + ", " + data.Bezeichnung + "[/Filename] konnte nicht eingelesen werden!";
                    }
                    else
                    {
                        this.LeseProtokoll = this.LeseProtokoll + "\n Konto[Filename]" + data.Konto + ", " + data.Bezeichnung + "[/Filename]";
                    }
                    this.LeseProtokoll = this.LeseProtokoll + "   [Bank]BMW-Bank-Datei[/Bank]";
                    this.LeseProtokoll = this.LeseProtokoll + "    [Number](" + Conversions.ToString(this.GetBMWKontoDetailsCount(Conversions.ToLong(data.Konto))) + " Datensätze)[/Number]\n";
                }
                if (!flag2)
                {
                    this.Zusammenfassung = "Einlesen der Daten fehlgeschlagen!";
                    this.ProtokollSymbol = "Exclamation";
                    this.LeseProtokoll = this.LeseProtokoll + "\n[Filename] Die weitere Verarbeitung wird abgebrochen![/Filename]";
                }
                kontenrundruf.Close();
                kontenrundruf.Dispose();
            }
            return flag2;
        }

        private fHeader GetHeader(string FileName)
        {
            fHeader header;
            header.ftType = FileTypes.ftUnknown;
            header.FileName = FileName;
            header.Deb_Kto_Nr = "";
            header.Name = "";
            header.Ort = "";
            header.LastChanged = "";
            switch (Path.GetExtension(FileName).ToUpper())
            {
                case ".CSV":
                {
                    int num;
                    string[] strArray;
                    int num2;
                    string[,] strArray2 = new string[0x65, 0x65];
                    using (TextFieldParser parser = new TextFieldParser(FileName))
                    {
                        parser.TextFieldType = FieldType.Delimited;
                        parser.SetDelimiters(new string[] { ";" });
                        for (num2 = 1; !parser.EndOfData & (num2 < 4); num2++)
                        {
                            strArray = parser.ReadFields();
                            num = 1;
                            foreach (string str in strArray)
                            {
                                strArray2[num2, num] = str;
                                num++;
                            }
                        }
                    }
                    if (((strArray2[1, 1] != null) & (strArray2[1, 2] != null)) && (strArray2[1, 1].Trim().StartsWith("INTERNE", StringComparison.OrdinalIgnoreCase) & strArray2[1, 2].Trim().StartsWith("KUNDE", StringComparison.OrdinalIgnoreCase)))
                    {
                        header.ftType = FileTypes.ftF1;
                        header.Name = StringTools.IsolateFileName(FileName);
                        header.FileName = FileName;
                    }
                    if (((strArray2[1, 1] != null) & (strArray2[2, 1] != null)) && (strArray2[1, 1].Trim().StartsWith("DEBITOREN", StringComparison.OrdinalIgnoreCase) & strArray2[2, 1].Trim().StartsWith("H", StringComparison.OrdinalIgnoreCase)))
                    {
                        header.ftType = FileTypes.ftBMW;
                        header.Name = StringTools.IsolateFileName(FileName);
                        header.FileName = FileName;
                        header.Ort = strArray2[3, 1].Substring(strArray2[3, 1].IndexOf(":") + 1).Trim();
                        header.Deb_Kto_Nr = strArray2[1, 1].Substring(strArray2[1, 1].IndexOf(":") + 1).Trim();
                    }
                    if (header.ftType == FileTypes.ftUnknown)
                    {
                        using (TextFieldParser parser2 = new TextFieldParser(FileName))
                        {
                            parser2.TextFieldType = FieldType.FixedWidth;
                            parser2.SetFieldWidths(new int[] { 12, 0x13, 9, 6, 0x10, 5, 14, 0x10, 0x13, -1 });
                            for (num2 = 1; !parser2.EndOfData & (num2 < 10); num2++)
                            {
                                try
                                {
                                    strArray = parser2.ReadFields();
                                    num = 1;
                                    foreach (string str in strArray)
                                    {
                                        strArray2[num2, num] = str;
                                        num++;
                                    }
                                }
                                catch (MalformedLineException exception1)
                                {
                                    ProjectData.SetProjectError(exception1);
                                    MalformedLineException exception = exception1;
                                    ProjectData.ClearProjectError();
                                }
                            }
                            if (((strArray2[5, 1] != null) & (strArray2[5, 2] != null)) && (strArray2[5, 1].Trim().StartsWith("VK", StringComparison.OrdinalIgnoreCase) & strArray2[5, 2].Trim().StartsWith("FAHRGEST", StringComparison.OrdinalIgnoreCase)))
                            {
                                header.ftType = FileTypes.ftBestand1;
                                header.Name = StringTools.IsolateFileName(FileName);
                                header.FileName = FileName;
                            }
                        }
                    }
                    if (header.ftType == FileTypes.ftUnknown)
                    {
                        using (TextFieldParser parser3 = new TextFieldParser(FileName))
                        {
                            parser3.TextFieldType = FieldType.FixedWidth;
                            parser3.SetFieldWidths(new int[] { 8, 13, 0x11, 10, 12, 11, 5, 1, 12, 14, 12, -1 });
                            for (num2 = 1; !parser3.EndOfData & (num2 < 10); num2++)
                            {
                                try
                                {
                                    strArray = parser3.ReadFields();
                                    num = 1;
                                    foreach (string str in strArray)
                                    {
                                        strArray2[num2, num] = str;
                                        num++;
                                    }
                                }
                                catch (MalformedLineException exception5)
                                {
                                    ProjectData.SetProjectError(exception5);
                                    MalformedLineException exception2 = exception5;
                                    ProjectData.ClearProjectError();
                                }
                            }
                            if (((strArray2[5, 1] != null) & (strArray2[5, 2] != null)) && (strArray2[5, 1].Trim().StartsWith("MODEL", StringComparison.OrdinalIgnoreCase) & strArray2[5, 2].Trim().StartsWith("BUCHNU", StringComparison.OrdinalIgnoreCase)))
                            {
                                header.ftType = FileTypes.ftBestand2;
                                header.Name = StringTools.IsolateFileName(FileName);
                                header.FileName = FileName;
                            }
                        }
                    }
                    if (header.ftType == FileTypes.ftUnknown)
                    {
                        using (TextFieldParser parser4 = new TextFieldParser(FileName))
                        {
                            parser4.TextFieldType = FieldType.FixedWidth;
                            parser4.SetFieldWidths(new int[] { 11, 15, 0x13, 12, 8, 10, 0x11, 4, 12, 11, -1 });
                            for (num2 = 1; !parser4.EndOfData & (num2 < 10); num2++)
                            {
                                try
                                {
                                    strArray = parser4.ReadFields();
                                    num = 1;
                                    foreach (string str in strArray)
                                    {
                                        strArray2[num2, num] = str;
                                        num++;
                                    }
                                }
                                catch (MalformedLineException exception6)
                                {
                                    ProjectData.SetProjectError(exception6);
                                    MalformedLineException exception3 = exception6;
                                    ProjectData.ClearProjectError();
                                }
                            }
                            if (((strArray2[5, 1] != null) & (strArray2[5, 2] != null)) && (strArray2[5, 1].Trim().StartsWith("VK", StringComparison.OrdinalIgnoreCase) & strArray2[5, 2].Trim().StartsWith("ORDER", StringComparison.OrdinalIgnoreCase)))
                            {
                                header.ftType = FileTypes.ftBestand3;
                                header.Name = StringTools.IsolateFileName(FileName);
                                header.FileName = FileName;
                            }
                        }
                    }
                    break;
                }
                case ".XLS":
                {
                    DataFile file = new DataFile();
                    file.FileName = FileName;
                    file.ColLimit = 0x19;
                    file.RowLimit = 12;
                    file.DoHeaderDetect = true;
                    file.DetectionLimit = 10;
                    file.ExplicitDetect = false;
                    file.HeaderToDetect = new string[] { "kreditnr", "datum", "fahrgest", "saldo" };
                    file.XlsWorksheet = 1;
                    bool flag = true;
                    try
                    {
                        file.Read();
                    }
                    catch (dfNoHeaderException exception7)
                    {
                        ProjectData.SetProjectError(exception7);
                        dfNoHeaderException exception4 = exception7;
                        flag = false;
                        ProjectData.ClearProjectError();
                    }
                    if (flag)
                    {
                        header.ftType = FileTypes.ftBMW_XLS;
                        header.Name = StringTools.IsolateFileName(FileName);
                        header.FileName = FileName;
                    }
                    break;
                }
            }
            if (header.ftType != FileTypes.ftUnknown)
            {
                header.LastChanged = Strings.Format(File.GetLastWriteTime(FileName), "dd.MM.yyyy HH:mm");
            }
            return header;
        }

        public bool ReadFiles(string Path)
        {
            bool flag2 = false;
            bool flag = true;
            this.LeseProtokoll = "";
            if (!this.GetFilesINet())
            {
                flag = false;
                flag2 = false;
            }
            if (!flag)
            {
                return flag2;
            }
            if (Directory.Exists(Path))
            {
                MyProject.Forms.Main.StatusProgress.Maximum = Directory.GetFiles(Path).Length;
                this.LeseProtokoll = this.LeseProtokoll + "Lese Dateien in [Directory]" + Path + "[/Directory]\n\n";
                foreach (string str in Directory.GetFiles(Path))
                {
                    fHeader fHdr = this.GetHeader(str);
                    this.LeseProtokoll = this.LeseProtokoll + "Lese [Filename]" + System.IO.Path.GetFileName(str) + "[/Filename]\n";
                    flag = true;
                    if (fHdr.ftType == FileTypes.ftBMW)
                    {
                        IEnumerator enumerator = this.Table_BMW_Debitorenkonten.Rows.GetEnumerator();
                        try
                        {
                            
                            while (enumerator.MoveNext())
                            {
                                DatenschemaBMW.TAB_BMW_DebitorenkontenRow current = (DatenschemaBMW.TAB_BMW_DebitorenkontenRow) enumerator.Current;
                                if (current.Abrufen && (current.Deb_Kto_Nr.ToString().Trim() == fHdr.Deb_Kto_Nr))
                                {
                                    this.LeseProtokoll = this.LeseProtokoll + "\n   [Bank]BMW-Bank-Datei " + fHdr.Deb_Kto_Nr + "[/Bank]\n-> übersprungen, wurde bereits per Internet abgerufen.";
                                    flag = false;
                                    goto Label_014B;
                                }
                            }
                        }
                        finally
                        {
                            if (enumerator is IDisposable)
                            {
                                (enumerator as IDisposable).Dispose();
                            }
                        }
                    }
                Label_014B:
                    if (flag)
                    {
                        this.LeseProtokoll = this.LeseProtokoll + "Lese [Filename]" + System.IO.Path.GetFileName(str) + "[/Filename]\n";
                        switch (fHdr.ftType)
                        {
                            case FileTypes.ftUnknown:
                                this.LeseProtokoll = this.LeseProtokoll + "   [unknown]unbekannte Datei[/unknown]\n";
                                break;

                            case FileTypes.ftF1:
                                this.LeseProtokoll = this.LeseProtokoll + "   [F1]Formel-1-Datei[/F1]";
                                break;

                            case FileTypes.ftBMW:
                                this.LeseProtokoll = this.LeseProtokoll + "   [Bank]BMW-Bank-Datei[/Bank]";
                                break;

                            case FileTypes.ftBMW_XLS:
                                this.LeseProtokoll = this.LeseProtokoll + "   [Bank]Kontoauszug[/Bank]";
                                break;

                            case FileTypes.ftBestand1:
                                this.LeseProtokoll = this.LeseProtokoll + "   [F1]Bestands-Datei (Neufahrzeuge)[/F1]";
                                break;

                            case FileTypes.ftBestand2:
                                this.LeseProtokoll = this.LeseProtokoll + "   [F1]Bestands-Datei (Gebrauchtfahrzeuge)[/F1]";
                                break;

                            case FileTypes.ftBestand3:
                                this.LeseProtokoll = this.LeseProtokoll + "   [F1]Bestands-Datei (Vorführfahrzeuge)[/F1]";
                                break;
                        }
                        if (fHdr.ftType != FileTypes.ftUnknown)
                        {
                            long num2 = this.SetHeader(fHdr);
                            int num = this.SetDetails((ulong) num2, fHdr);
                            this.LeseProtokoll = this.LeseProtokoll + "    [Number](" + Conversions.ToString(num) + " Datensätze)[/Number]\n";
                            MyProject.Forms.Main.ClearTreeView();
                            MyProject.Forms.Main.FillTreeView();
                        }
                    }
                    MyProject.Forms.Main.StatusProgress.PerformStep();
                    MyProject.Forms.Main.Refresh();
                    this.LeseProtokoll = this.LeseProtokoll + "\n";
                }
                return true;
            }
            return false;
        }

        public void SessionClose()
        {
            ProjectData.EndApp();
        }

        public void SessionLoad()
        {

            this.dsBMW.Clear();
            this.dsBMW.ReadXml(MySettings.MySettingsProperty.DatabasePath + @"\amDataBase.xml");
            this.ClearAbloeseHistorie();
            object left = false;
            if (this.dsBMW.TAB_AuswertungTitle.Rows.Count == 0)
            {
                left = true;
            }
            else
            {
                try
                {
                    if (Operators.ConditionalCompareObjectLess(this.dsBMW.TAB_AuswertungTitle.Rows[0]["DataBaseVersion"], this.DataBaseNeededVersion, false))
                    {
                        left = true;
                    }
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    left = true;
                    ProjectData.ClearProjectError();
                }
            }
            if (Operators.ConditionalCompareObjectEqual(left, true, false))
            {
                Interaction.MsgBox("Ihre Datendatei ist mit dieser Programmversion nicht mehr kompatibel! Bitte machen Sie ein Backup Ihrer momentanen Datendatei und löschen Sie sie dann, damit das Programm eine neue Datendatei in diesem Pfad anlegen kann.", MsgBoxStyle.Critical, "Alte Datendatei nach Programm-Update");
                ProjectData.EndApp();
            }
            IEnumerator enumerator = this.Table_Finanzierungsarten.Rows.GetEnumerator();
            try
            {
                
                while (enumerator.MoveNext())
                {
                    DataRow current = (DataRow) enumerator.Current;
                    if (current["FA_Tage_Auto"] == DBNull.Value)
                    {
                        current["FA_Tage_Auto"] = 0;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            this.dsBMW.AcceptChanges();
            File.Move(MySettings.MySettingsProperty.DatabasePath + @"\amDataBase.xml", MySettings.MySettingsProperty.DatabasePath + @"\amDataBase.locked");
        }

        public void SessionSave()
        {
            if (this.dsBMW.TAB_AuswertungTitle.Rows.Count == 0)
            {
                DataRow row = this.dsBMW.TAB_AuswertungTitle.NewRow();
                row["DataBaseVersion"] = RuntimeHelpers.GetObjectValue(this.DataBaseNeededVersion);
                this.dsBMW.TAB_AuswertungTitle.Rows.Add(row);
            }
            else
            {
                this.dsBMW.TAB_AuswertungTitle.Rows[0]["DataBaseVersion"] = RuntimeHelpers.GetObjectValue(this.DataBaseNeededVersion);
            }
            this.dsBMW.AcceptChanges();
            this.dsBMW.WriteXml(MySettings.MySettingsProperty.DatabasePath + @"\amDataBase.xml");
            if (File.Exists(MySettings.MySettingsProperty.DatabasePath + @"\amDataBase.locked"))
            {
                File.Delete(MySettings.MySettingsProperty.DatabasePath + @"\amDataBase.locked");
            }
        }

        public void SetCompareFile()
        {
            DataRow row;
            DataRow row2;
            DataRow current;
            bool flag;
       
            int num = 0;
            int num2 = 0;
            int num5 = 0;
            int num3 = 0;
            int num4 = 0;
            this.dsBMW.TAB_Auswertung.Clear();
            MyProject.Forms.Main.StatusProgress.Maximum = this.dsBMW.TAB_F1_Detail.Rows.Count + this.dsBMW.TAB_Bestand_Details.Rows.Count;
            IEnumerator enumerator = this.dsBMW.TAB_F1_Detail.Rows.GetEnumerator();
            try
            {
                
                while (enumerator.MoveNext())
                {
                    IEnumerator enumerator2 = this.dsBMW.TAB_BMW_Bank_Details.Rows.GetEnumerator();
                    current = (DataRow) enumerator.Current;
                    flag = false;
                    num4 = 0;
                    try
                    {
                        
                        while (enumerator2.MoveNext())
                        {
                            row2 = (DataRow) enumerator2.Current;
                            if ((row2["Kredit_Saldo"] != DBNull.Value) && Conversions.ToBoolean(Operators.AndObject(Operators.AndObject(Operators.AndObject(current["FGST_NR"].ToString().Trim().EndsWith(row2["FGST_Nr"].ToString().Trim()), Operators.CompareObjectGreater(row2["Kredit_Saldo"], 0, false)), current["FGST_NR"] != DBNull.Value), current["FGST_NR"].ToString().Trim() != "")))
                            {
                                flag = true;
                                row = this.dsBMW.TAB_Auswertung.NewRow();
                                row["FGST_Nr"] = RuntimeHelpers.GetObjectValue(row2["FGST_NR"]);
                                row["Name1"] = RuntimeHelpers.GetObjectValue(current["Name1"]);
                                row["RE_Dat_VK"] = RuntimeHelpers.GetObjectValue(current["RE_Dat_VK"]);
                                row["RE_Nr_Vk"] = RuntimeHelpers.GetObjectValue(current["RE_Nr_Vk"]);
                                row["Kunden_Nr"] = RuntimeHelpers.GetObjectValue(current["Kunde_Gekauft"]);
                                row["Kredit_Nr"] = RuntimeHelpers.GetObjectValue(row2["Kredit_Nr"]);
                                row["Kredit_Saldo"] = RuntimeHelpers.GetObjectValue(row2["Kredit_Saldo"]);
                                row["F1_Header_ID"] = RuntimeHelpers.GetObjectValue(current["F1_Header_ID"]);
                                row["BMW_Header_ID"] = RuntimeHelpers.GetObjectValue(row2["BMW_Header_ID"]);
                                current["HasBank"] = true;
                                bool flag2 = false;
                                DataRow[] rowArray2 = this.dsBMW.TAB_FinanzArten.Select(Conversions.ToString(Operators.ConcatenateObject(Operators.ConcatenateObject("FA_KundeAuto = '", row["Kunden_Nr"]), "'")));
                                if (rowArray2.Length > 0)
                                {
                                    row["FA_ID"] = RuntimeHelpers.GetObjectValue(rowArray2[0]["FA_ID"]);
                                    num2++;
                                    flag2 = true;
                                }
                                DataRow[] rowArray = this.dsBMW.TAB_FinanzArten.Select("FA_Tage_Auto >0");
                                if (rowArray.Length > 0)
                                {
                                    DateTime time2 = DateAndTime.DateAdd("d", Conversions.ToDouble(rowArray[0]["FA_Tage_Auto"]), RuntimeHelpers.GetObjectValue(row["RE_Dat_VK"]));
                                    num2++;
                                    if (DateTime.Compare(time2, DateAndTime.Now) <= 0)
                                    {
                                        row["FA_ID"] = RuntimeHelpers.GetObjectValue(rowArray[0]["FA_ID"]);
                                        num2++;
                                        flag2 = true;
                                    }
                                }
                                if (!flag2)
                                {
                                    row["FA_ID"] = DBNull.Value;
                                }
                                num++;
                                this.dsBMW.TAB_Auswertung.Rows.Add(row);
                                goto Label_03F2;
                            }
                            num4++;
                        }
                    }
                    finally
                    {
                        if (enumerator2 is IDisposable)
                        {
                            (enumerator2 as IDisposable).Dispose();
                        }
                    }
                Label_03F2:
                    MyProject.Forms.Main.StatusProgress.PerformStep();
                    num5++;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            IEnumerator enumerator3 = this.dsBMW.TAB_Bestand_Details.GetEnumerator();
            try
            {
                while (enumerator3.MoveNext())
                {
                    current = (DataRow) enumerator3.Current;
                    flag = false;
                    IEnumerator enumerator4 = this.dsBMW.TAB_BMW_Bank_Details.Rows.GetEnumerator();
                    try
                    {
                        
                        while (enumerator4.MoveNext())
                        {
                            row2 = (DataRow) enumerator4.Current;
                            if ((row2["Kredit_Saldo"] != DBNull.Value) && Conversions.ToBoolean(Operators.AndObject(Operators.AndObject(Operators.AndObject(current["FGST_NR"].ToString().Trim().EndsWith(row2["FGST_Nr"].ToString().Trim()), Operators.CompareObjectGreater(row2["Kredit_Saldo"], 0, false)), current["FGST_NR"] != DBNull.Value), current["FGST_NR"].ToString().Trim() != "")))
                            {
                                flag = true;
                                break;
                            }
                            if (!flag)
                            {
                                row = this.dsBMW.TAB_Auswertung_Bestand.NewRow();
                                row["FGST_Nr"] = Strings.Right(Conversions.ToString(current["FGST_NR"]), 7);
                                row["Datum"] = RuntimeHelpers.GetObjectValue(current["Datum"]);
                                row["EK-Preis"] = RuntimeHelpers.GetObjectValue(current["EK-Preis"]);
                                row["Bestand_Header_ID"] = RuntimeHelpers.GetObjectValue(current["Bestand_Header_ID"]);
                                this.dsBMW.TAB_Auswertung_Bestand.Rows.Add(row);
                                MyProject.Forms.Main.StatusProgress.PerformStep();
                                num3++;
                            }
                        }
                        continue;
                    }
                    finally
                    {
                        if (enumerator4 is IDisposable)
                        {
                            (enumerator4 as IDisposable).Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (enumerator3 is IDisposable)
                {
                    (enumerator3 as IDisposable).Dispose();
                }
            }
            MyProject.Forms.frmAuswertung.InitGridColors();
            this.BemerkungenLesen();
            this.DauerFAsLesen();
            if (((num5 > 0) | (num4 > 0)) | (num3 > 0))
            {
                this.Zusammenfassung = "Verarbeitet:\n    Fakturierte Fahrzeuge: " + Conversions.ToString(num5) + " Datensätze\n    Bestandsfahrzeuge: " + Conversions.ToString(num3) + " Datensätze\n    Kontoauszüge: " + Conversions.ToString(num4) + " Datensätze\n\nEs wurden " + Conversions.ToString(num) + " Datensätze für die Auswertung erzeugt.\nDavon wurden " + Conversions.ToString(num2) + " Datensätzen automatisch Finanzierungsarten zugewiesen.";
                this.ProtokollSymbol = "Information";
            }
            else
            {
                this.Zusammenfassung = "Es wurden keine lesbaren Dateien gefunden. Bitte kontrollieren Sie Ihre Pfadeinstellungen.";
                this.ProtokollSymbol = "Exclamation";
            }
        }

        private int SetDetails(ulong lngHdId, fHeader fHdr)
        {
            int num = 0;
            switch (fHdr.ftType)
            {
                case FileTypes.ftF1:
                case FileTypes.ftBMW:
                {
                    bool flag = false;
                    int num2 = 0;
                    using (TextFieldParser parser = new TextFieldParser(fHdr.FileName, Encoding.Default))
                    {
                        parser.TextFieldType = FieldType.Delimited;
                        parser.SetDelimiters(new string[] { ";" });
                        while (!parser.EndOfData)
                        {
                            string[] strArray = parser.ReadFields();
                            if (flag)
                            {
                                DataRow row;
                                num2++;
                                if (fHdr.ftType == FileTypes.ftF1)
                                {
                                    row = this.dsBMW.TAB_F1_Detail.NewRow();
                                    row["F1_Header_ID"] = lngHdId;
                                    try
                                    {
                                        row["Interne_Nr"] = strArray[0];
                                        row["Kunden_Nr"] = strArray[1];
                                        row["FGST_Nr"] = strArray[2];
                                        row["Name1"] = strArray[3];
                                        if (Information.IsDate(strArray[4]))
                                        {
                                            row["RE_Dat_VK"] = Strings.Format(Conversions.ToDate(strArray[4]), "dd.MM.yyyy");
                                        }
                                        else
                                        {
                                            row["RE_Dat_VK"] = strArray[4];
                                        }
                                        row["RE_Nr_VK"] = strArray[5];
                                        row["Kunde_Gekauft"] = strArray[6];
                                    }
                                    catch (Exception exception1)
                                    {
                                        ProjectData.SetProjectError(exception1);
                                        ProjectData.ClearProjectError();
                                    }
                                    finally
                                    {
                                        this.dsBMW.TAB_F1_Detail.Rows.Add(row);
                                    }
                                }
                                if (fHdr.ftType == FileTypes.ftBMW)
                                {
                                    row = this.dsBMW.TAB_BMW_Bank_Details.NewRow();
                                    row["BMW_Header_ID"] = lngHdId;
                                    try
                                    {
                                        row["Kredit_Nr"] = strArray[0];
                                        row["FGST_Nr"] = strArray[1];
                                        if (Information.IsDate(strArray[2]))
                                        {
                                            row["Kredit_Beginn"] = Strings.Format(Conversions.ToDate(strArray[2]), "dd.MM.yyyy");
                                        }
                                        else
                                        {
                                            row["Kredit_Beginn"] = strArray[2];
                                        }
                                        row["FA_Art"] = strArray[3];
                                        row["FZG_Typ"] = strArray[4];
                                        row["Kredit_Saldo"] = Conversions.ToDouble(strArray[5]);
                                    }
                                    catch (Exception exception2)
                                    {
                                        ProjectData.SetProjectError(exception2);
                                        ProjectData.ClearProjectError();
                                    }
                                    finally
                                    {
                                        this.dsBMW.TAB_BMW_Bank_Details.Rows.Add(row);
                                    }
                                }
                            }
                            if (!flag & (strArray[0].Trim().StartsWith("KREDITNU", StringComparison.OrdinalIgnoreCase) | strArray[0].Trim().StartsWith("INTERNE", StringComparison.OrdinalIgnoreCase)))
                            {
                                flag = true;
                            }
                        }
                    }
                    return num2;
                }
                case FileTypes.ftBMW_XLS:
                {
                    
                    DataFile file = new DataFile();
                    file.FileName = fHdr.FileName;
                    file.ColLimit = 0x19;
                    file.DoHeaderDetect = true;
                    file.DetectionLimit = 10;
                    file.ExplicitDetect = false;
                    file.HeaderToDetect = new string[] { "kreditnr", "datum", "fahrgest", "saldo" };
                    file.XlsWorksheet = 1;
                    file.Read();
                    int num6 = 0;

                    IEnumerator enumerator = file.Data.Rows.GetEnumerator();
                    try
                    {
                        while (enumerator.MoveNext())
                        {
                            DataRow current = (DataRow) enumerator.Current;
                            num6++;
                            DataRow row5 = this.dsBMW.TAB_BMW_Bank_Details.NewRow();
                            row5["BMW_Header_ID"] = lngHdId;
                            try
                            {
                                row5["Kredit_Nr"] = RuntimeHelpers.GetObjectValue(current["kreditnr"]);
                                row5["FGST_Nr"] = RuntimeHelpers.GetObjectValue(current["fahrgest"]);
                                if (Information.IsDate(RuntimeHelpers.GetObjectValue(current["datum"])))
                                {
                                    row5["Kredit_Beginn"] = Strings.Format(Conversions.ToDate(current["datum"]), "dd.MM.yyyy");
                                }
                                else
                                {
                                    row5["Kredit_Beginn"] = RuntimeHelpers.GetObjectValue(current["datum"]);
                                }
                                row5["FA_Art"] = "";
                                row5["FZG_Typ"] = "";
                                row5["Kredit_Saldo"] = Conversions.ToDouble(current["saldo"]);
                                continue;
                            }
                            catch (Exception exception6)
                            {
                                ProjectData.SetProjectError(exception6);
                                ProjectData.ClearProjectError();
                                continue;
                            }
                            finally
                            {
                                this.dsBMW.TAB_BMW_Bank_Details.Rows.Add(row5);
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator is IDisposable)
                        {
                            (enumerator as IDisposable).Dispose();
                        }
                    }
                    return num6;
                }
                case FileTypes.ftBestand1:
                {
                    int num3 = 0;
                    using (TextFieldParser parser2 = new TextFieldParser(fHdr.FileName, Encoding.Default))
                    {
                        parser2.TextFieldType = FieldType.FixedWidth;
                        parser2.SetFieldWidths(new int[] { 12, 0x13, 9, 6, 0x10, 5, 14, 0x10, 0x13, -1 });
                        while (!parser2.EndOfData)
                        {
                            try
                            {
                                string[] strArray2 = parser2.ReadFields();
                                if (Information.IsDate(strArray2[9]))
                                {
                                    num3++;
                                    DataRow row2 = this.dsBMW.TAB_Bestand_Details.NewRow();
                                    row2["Bestand_Header_ID"] = lngHdId;
                                    row2["FGST_Nr"] = strArray2[1];
                                    row2["Datum"] = Strings.Format(Conversions.ToDate(strArray2[9]), "dd.MM.yyyy");
                                    row2["EK-Preis"] = strArray2[8];
                                    this.dsBMW.TAB_Bestand_Details.Rows.Add(row2);
                                }
                                continue;
                            }
                            catch (Exception exception3)
                            {
                                ProjectData.SetProjectError(exception3);
                                ProjectData.ClearProjectError();
                                continue;
                            }
                        }
                    }
                    return num3;
                }
                case FileTypes.ftBestand2:
                {
                    int num4 = 0;
                    using (TextFieldParser parser3 = new TextFieldParser(fHdr.FileName, Encoding.Default))
                    {
                        parser3.TextFieldType = FieldType.FixedWidth;
                        parser3.SetFieldWidths(new int[] { 8, 13, 0x11, 10, 12, 11, 5, 1, 12, 14, 12, -1 });
                        while (!parser3.EndOfData)
                        {
                            try
                            {
                                string[] strArray3 = parser3.ReadFields();
                                if (Information.IsDate(strArray3[5]))
                                {
                                    num4++;
                                    DataRow row3 = this.dsBMW.TAB_Bestand_Details.NewRow();
                                    row3["Bestand_Header_ID"] = lngHdId;
                                    row3["FGST_Nr"] = strArray3[2];
                                    row3["Datum"] = Strings.Format(Conversions.ToDate(strArray3[5]), "dd.MM.yyyy");
                                    row3["EK-Preis"] = strArray3[9];
                                    this.dsBMW.TAB_Bestand_Details.Rows.Add(row3);
                                }
                                continue;
                            }
                            catch (Exception exception4)
                            {
                                ProjectData.SetProjectError(exception4);
                                ProjectData.ClearProjectError();
                                continue;
                            }
                        }
                    }
                    return num4;
                }
                case FileTypes.ftBestand3:
                {
                    int num5 = 0;
                    using (TextFieldParser parser4 = new TextFieldParser(fHdr.FileName, Encoding.Default))
                    {
                        parser4.TextFieldType = FieldType.FixedWidth;
                        parser4.SetFieldWidths(new int[] { 11, 15, 0x13, 12, 8, 10, 0x11, 4, 12, 11, -1 });
                        while (!parser4.EndOfData)
                        {
                            try
                            {
                                string[] strArray4 = parser4.ReadFields();
                                if (Information.IsDate(strArray4[4]))
                                {
                                    num5++;
                                    DataRow row4 = this.dsBMW.TAB_Bestand_Details.NewRow();
                                    row4["Bestand_Header_ID"] = lngHdId;
                                    row4["FGST_Nr"] = strArray4[2];
                                    row4["Datum"] = Strings.Format(Conversions.ToDate(strArray4[4]), "dd.MM.yyyy");
                                    row4["EK-Preis"] = strArray4[10];
                                    this.dsBMW.TAB_Bestand_Details.Rows.Add(row4);
                                }
                                continue;
                            }
                            catch (Exception exception5)
                            {
                                ProjectData.SetProjectError(exception5);
                                ProjectData.ClearProjectError();
                                continue;
                            }
                        }
                    }
                    return num5;
                }
            }
            return num;
        }

        private long SetHeader(fHeader fHdr)
        {
            DataRow row;
            long num = 0L;
            switch (fHdr.ftType)
            {
                case FileTypes.ftF1:
                    row = this.dsBMW.TAB_F1_Header.NewRow();
                    row["Name"] = fHdr.Name;
                    row["Filename"] = fHdr.FileName;
                    row["LastChanged"] = fHdr.LastChanged;
                    row["Typ"] = "Fakturiert";
                    this.dsBMW.TAB_F1_Header.Rows.Add(row);
                    return Conversions.ToLong(row["F1_Header_ID"]);

                case FileTypes.ftBMW:
                    row = this.dsBMW.TAB_BMW_Bank_Header.NewRow();
                    row["Name"] = fHdr.Name;
                    row["Filename"] = fHdr.FileName;
                    row["LastChanged"] = fHdr.LastChanged;
                    row["Deb_Kto_Nr"] = fHdr.Deb_Kto_Nr;
                    row["Ort"] = fHdr.Ort;
                    row["Typ"] = "BMW";
                    this.dsBMW.TAB_BMW_Bank_Header.Rows.Add(row);
                    return Conversions.ToLong(row["BMW_Header_ID"]);

                case FileTypes.ftBMW_XLS:
                    row = this.dsBMW.TAB_BMW_Bank_Header.NewRow();
                    row["Name"] = fHdr.Name;
                    row["Filename"] = fHdr.FileName;
                    row["LastChanged"] = fHdr.LastChanged;
                    row["Typ"] = "Andere";
                    this.dsBMW.TAB_BMW_Bank_Header.Rows.Add(row);
                    return Conversions.ToLong(row["BMW_Header_ID"]);

                case FileTypes.ftBestand1:
                case FileTypes.ftBestand2:
                case FileTypes.ftBestand3:
                    row = this.dsBMW.TAB_Bestand_Header.NewRow();
                    row["Name"] = fHdr.Name;
                    row["Filename"] = fHdr.FileName;
                    row["LastChanged"] = fHdr.LastChanged;
                    switch (fHdr.ftType)
                    {
                        case FileTypes.ftBestand1:
                            row["Typ"] = "N";
                            goto Label_0148;

                        case FileTypes.ftBestand2:
                            row["Typ"] = "G";
                            goto Label_0148;

                        case FileTypes.ftBestand3:
                            row["Typ"] = "V";
                            goto Label_0148;
                    }
                    break;

                default:
                    return num;
            }
        Label_0148:
            this.dsBMW.TAB_Bestand_Header.Rows.Add(row);
            return Conversions.ToLong(row["Bestand_Header_ID"]);
        }

        public string AuswertungName
        {
            get
            {
                if (this.dsBMW.TAB_AuswertungTitle.Rows.Count > 0)
                {
                    if (this.dsBMW.TAB_AuswertungTitle[0]["Title"] != DBNull.Value)
                    {
                        return Conversions.ToString(this.dsBMW.TAB_AuswertungTitle[0]["Title"]);
                    }
                    return "";
                }
                return "";
            }
            set
            {
                if (this.dsBMW.TAB_AuswertungTitle.Rows.Count > 0)
                {
                    this.dsBMW.TAB_AuswertungTitle[0]["Title"] = value;
                }
                else
                {
                    DataRow row = this.dsBMW.TAB_AuswertungTitle.NewRow();
                    row["Title"] = value;
                    this.dsBMW.TAB_AuswertungTitle.Rows.Add(row);
                }
            }
        }

        internal string Manufacturer
        {
            get
            {
                return "";
            }
        }

        internal string ProductName
        {
            get
            {
                return "Solvo\r\nAblösesteuerung für BMW-Autohäuser";
            }
        }

        internal string SerialNo
        {
            get
            {
                return "050100";
            }
        }

        public DataTable Table_Auswertung
        {
            get
            {
                return this.dsBMW.TAB_Auswertung;
            }
        }

        public DataTable Table_Auswertung_Bestand
        {
            get
            {
                return this.dsBMW.TAB_Auswertung_Bestand;
            }
        }

        public DataTable Table_BestandDetails
        {
            get
            {
                return this.dsBMW.TAB_Bestand_Details;
            }
        }

        public DataTable Table_BestandHeaders
        {
            get
            {
                return this.dsBMW.TAB_Bestand_Header;
            }
        }

        public DataTable Table_BMW_Bank_Details_Alle_TMP
        {
            get
            {
                return this.dsBMW.TAB_BMW_Bank_Details_Alle_TMP;
            }
        }

        public DataTable Table_BMW_Debitorenkonten
        {
            get
            {
                return this.dsBMW.TAB_BMW_Debitorenkonten;
            }
        }

        public DataTable Table_BMWBankDetails
        {
            get
            {
                return this.dsBMW.TAB_BMW_Bank_Details;
            }
        }

        public DataTable Table_BMWBankHeaders
        {
            get
            {
                return this.dsBMW.TAB_BMW_Bank_Header;
            }
        }

        public DataTable Table_F1Details
        {
            get
            {
                return this.dsBMW.TAB_F1_Detail;
            }
        }

        public DataTable Table_F1Headers
        {
            get
            {
                return this.dsBMW.TAB_F1_Header;
            }
        }

        public DataTable Table_Finanzierungsarten
        {
            get
            {
                return this.dsBMW.TAB_FinanzArten;
            }
        }

        public DataTable Table_Historie
        {
            get
            {
                return this.dsBMW.TAB_Historie;
            }
        }

        internal string Version
        {
            get
            {
                string str = "";
                System.Version version = MyProject.Application.Info.Version;
                str = version.Major.ToString() + "." + version.Minor.ToString() + "." + version.Build.ToString();
                version = null;
                return str;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct fHeader
        {
            public Session.FileTypes ftType;
            public string Name;
            public string FileName;
            public string Deb_Kto_Nr;
            public string Ort;
            public string LastChanged;
        }

        public enum FileTypes
        {
            ftBestand1 = 4,
            ftBestand2 = 5,
            ftBestand3 = 6,
            ftBMW = 2,
            ftBMW_XLS = 3,
            ftF1 = 1,
            ftUnknown = -1
        }
    }
}

